package com.guan.ordering_sys.generator.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guan.ordering_sys.common.PageResult;
import com.guan.ordering_sys.common.StateCode;
import com.guan.ordering_sys.exception.BaseException;
import com.guan.ordering_sys.generator.entity.Employee;
import com.guan.ordering_sys.generator.mapper.EmployeeMapper;
import com.guan.ordering_sys.generator.service.CompanyService;
import com.guan.ordering_sys.generator.service.EmployeePostService;
import com.guan.ordering_sys.generator.service.EmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guan.ordering_sys.generator.service.PostService;
import com.guan.ordering_sys.param.PageParam;
import com.guan.ordering_sys.vo.CompanyVo;
import com.guan.ordering_sys.vo.EmployeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  EmployeeServiceImpl服务实现类
 * </p>
 *
 * @author 关远键
 * @version v1.0.3
>>>>>>> dbd4864607b6833095bbd9f50ba1f6c0de03bbbc
 * @since 2021-06-18
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private EmployeePostService epService;

    @Autowired
    private PostService postService;

    @Autowired
    private CompanyService companyService;


    /**
     * 分页雇员信息
     * @author 关远键
     * @date 2021/6/27 23:38
     * @param pageParam
     * @return com.guan.ordering_sys.common.PageResult
     */
    @Override
    public PageResult getList(PageParam pageParam) {
        Page<Employee> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        IPage<Employee> list = employeeMapper.getList(page);
        if (list.getSize() == 0) {
            throw new BaseException(StateCode.NOT_FOUND);
        }

        List<EmployeeVo> voList = new ArrayList<>();
        List<Employee> records = list.getRecords();
        for (Employee record : records) {
            Integer postId = epService.getPostIdByEmployeeId(record.getEmployeeId()).get(0);
            Long companyId = Long.valueOf(postService.selectById(postId).getCompanyId());
            CompanyVo companyVo = companyService.getById(companyId);
            voList.add(new EmployeeVo(companyVo, record));
        }

        Page<EmployeeVo> voPage = new Page<>();
        voPage.setRecords(voList);

        return new PageResult(voPage.getRecords().size(), voPage.getRecords());

    }

    /**
     * 根据雇员变化获取雇员信息
     * @author 关远键
     * @date 2021/6/28 10:38
     * @param id
     * @return com.guan.ordering_sys.generator.entity.Employee
     */
    @Override
    public Employee getById(Long id) {
        Employee employee = employeeMapper.selectById(id);
        if (employee == null) {
            throw new BaseException(StateCode.NOT_FOUND);
        }
        return employee;
    }

}
